<template>
  <div class="adjust-wrap-top">
    <div class="tab-change flex-row">
      <ul class="flex-row item">
        <li :class="{'cur1':pageSet=='1'}" @click="pageSet ='1'">
          <el-badge style='font-size: 15px' :value="wmNewOorder||''" :max="99" class="set-int">外卖
          </el-badge>
        </li>
        <li :class="{'cur2':pageSet=='2'}" @click="pageSet ='2'">
          <el-badge style='font-size: 15px' :value="ptNewOorder||''" :max="99" class="set-int">跑腿
          </el-badge>
        </li>
      </ul>
      <div class="for-set">
        <span>调度模式：</span>
        <span class="btns" v-show='pageSet=="1"'>
          <span class="list" :class="{'cur': orderMode === 1}" @click="changeOrderPattern(1, 1)">抢单</span>
          <span class="list" :class="{'cur': orderMode === 2}" @click="changeOrderPattern(1, 2)">派单</span>
          <span class="list" :class="{'cur': orderMode === 3}" @click="changeOrderPattern(1, 3)">智能</span>
        </span>
        <span class="btns" v-show='pageSet=="2"'>
          <span class="list" :class="{'cur': paotuiOrderMode === 1}" @click="changeOrderPattern(2, 1)">抢单</span>
          <span class="list" :class="{'cur': paotuiOrderMode === 2}" @click="changeOrderPattern(2, 2)">派单</span>
          <span class="list" :class="{'cur': paotuiOrderMode === 3}" @click="changeOrderPattern(2, 3)">智能</span>
        </span>
        <span style="margin-left: 50px;">订单语言提示：</span>
        <el-switch @change="handelChange" v-model="videoSet" active-color="#13ce66" />
      </div>
    </div>
   <adjust-O v-show='pageSet=="1"' ref="child1" :videoSet="videoSet" @wmWebsockerNum ="getDiaoduWmNum" />
   <adjust-T v-show='pageSet=="2"' ref="child2" :videoSet="videoSet" @ptWebsockerNum ="getDiaoduPtNum" />
  </div>
</template>

<script>
  import { setOrderModel } from '@/api/adjust'
  import adjustO from './components/adjust1'
  import adjustT from './components/adjust2'
  export default {
    components: { adjustO, adjustT },
    data() {
      return {
        pageSet: '1',
        ptNewOorder:0,
        wmNewOorder:0,
        videoSet: localStorage.getItem('videoSet')? JSON.parse(localStorage.getItem('videoSet')).videoSet: false,
        orderMode: null, //外卖抢单或派单
        paotuiOrderMode: null, // 跑腿抢单或派单
        isReady: false // 渲染订单模式是否完成
      }
    },
    watch: {
      '$store.state.agentList': {
        handler(val) {
          if (val && val.length) {
            if (val.length === 1) { // 只有一个区域 直接显示
              this.isReady = true
              this.orderMode = val[0].orderMode
              this.paotuiOrderMode = val[0].paotuiOrderMode
            }
          }
        },
        deep: true,
        immediate: true
      }
    },
    methods: {
      handelChange(val) {
        localStorage.setItem('videoSet', JSON.stringify({videoSet: val}))
      },
      getOrderType(type){
        this.orderType = type
      },
      getDiaoduWmNum(num){
        this.wmNewOorder = num
      },
      getDiaoduPtNum(num){
        this.ptNewOorder = num
      },
      changeOrderPattern(type, value) { //切换订单模式
        if (this.isReady) {
          const titleMsg = [ '是否开启抢单模式,开启后骑手可手动接单！', '是否开启派单模式, 开启后需后台手动对骑手派单!', '是否开启只能模式, 开启后系统自动对骑手派单!']
          this.$confirm(titleMsg[value - 1], '切换订单模式').then(() => {
            const params = {}
            if (type === 1) params.orderMode = value
            else params.paotuiOrderMode = value
            setOrderModel(params).then(res => {
              if (res.success) {
                this.$message.success('修改成功!')
                const agentList = [...this.$store.state.agentList]
                if (type === 1) {
                  agentList[0].orderMode = value
                  this.orderMode = value
                } else {
                  agentList[0].paotuiOrderMode = value
                  this.paotuiOrderMode = value
                }
                this.$store.commit('Set_agentList', agentList)
              }
            })
          }).catch(() => {
            this.$message.info('已取消切换订单操作')
          })
        } else {
          this.$message('页面未渲染完成，请稍等')
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .adjust-wrap-top{
    position: relative;
    top: -15px;
  }
  .tab-change{
    position: relative;
    left: -15px;
    width: calc(100% + 30px);
    height: 50px;
    justify-content: center;
    background: #f1f1f1;
    .item{
      height: 100%;
      li{
        min-width: 100px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-size: 15px;
        font-weight: bold;
        cursor: pointer;
        border-radius: 5px 5px 0 0;
        &.cur1{
          background: #fff;
          color: rgb(247, 93, 120);
        }
        &.cur2{
          background: #fff;
          color:rgb(95, 171, 247);
        }
      }
    }
  }
  /deep/ .set-int .el-badge__content.is-fixed{
    top:10px !important;
  }
  .for-set{
    position: absolute;
    right: 0;
    height: 50px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    .btns{
      display: flex;
      border-radius: 2px;
      .list{
        padding: 4px 8px;
        cursor: pointer;
        border: 1px solid #ccc;
        box-sizing: border-box;
        &.cur{
          background: red;
          color: #fff;
          border: 1px solid RED;
        }
        &:nth-child(even){
          border-left: none;
          border-right: none;
        }
      }
    }
  }
</style>